use dta/SMDLevel.dta, clear
sort year SMD
merge year SMD using dta/Turnout_District_Level_Sort.dta

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************

*********************** CREATE BLOCS **********************************************

foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	gen diff`round'_`bloc'=vs`round'_`bloc'-l3.vs`round'_`bloc'
	}
}

gen lag_turnout=l3.turnout

keep year SMD PR *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* diff* *turnout*
order year SMD PR vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF* 

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen AdvantageLibCon=(vs1_LIB-vs1_CON)/(vs1_CON+vs1_LIB)
gen AdvantageLibLab=(vs1_LIB-vs1_LAB)/(vs1_LIB+vs1_LAB)
gen AdvantageLabCon=(vs1_LAB-vs1_CON)/(vs1_LAB+vs1_CON)

foreach var in AdvantageLibCon AdvantageLibLab AdvantageLabCon {
gen lag_`var'=l3.`var'
}

sort SMD
merge SMD using dta/StableSample

sum lag_Advantage* if year==1921 & sample==1
corr lag_Advantage* if year==1921 & sample==1


**********************************************************************************
**********************************************************************************
**********************************************************************************

scatter lag_turnout lag_AdvantageLibCon if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Pre-reform turnout) xtitle(Pre-reform LIB advantage over CON)
graph save figures/gph/corr1.gph, replace
scatter lag_turnout lag_AdvantageLibLab if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Pre-reform turnout) xtitle(Pre-reform LIB advantage over LAB)
graph save figures/gph/corr2.gph, replace
scatter lag_turnout lag_AdvantageLabCon if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Pre-reform turnout) xtitle(Pre-reform LAB advantage over CON)
graph save figures/gph/corr3.gph, replace


scatter turnout lag_AdvantageLibCon if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Post-reform turnout) xtitle(Pre-reform LIB advantage over CON)
graph save figures/gph/corr4.gph, replace
scatter turnout lag_AdvantageLibLab if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Post-reform turnout) xtitle(Pre-reform LIB advantage over LAB)
graph save figures/gph/corr5.gph, replace
scatter turnout lag_AdvantageLabCon if sample==1 & year==1921, msymbol(Oh) scheme(s1mono) ytitle(Post-reform turnout) xtitle(Pre-reform LAB advantage over CON)
graph save figures/gph/corr6.gph, replace

cd figures/gph
graph combine corr1.gph corr2.gph corr3.gph corr4.gph corr5.gph corr6.gph, ycommon scheme(s1mono) 
graph export ../FigureA10.pdf, replace
cd ../../
